package com.wangyadong.hobby.front.model.regx;


import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.IntStream;

public class FirstJavaRegluar {
    static String myTest = "this is my 1st test string";

    static String myRex = "\\d+\\w+";


    public static void main(String[] args) throws UnsupportedEncodingException {

        String chinese="ï¿½ï¿½";
        String[] split = chinese.split("；");
        System.out.println(split.length);
//        test1(null);
//        testEmail(null);
//        testEmailjianjian(null);
//        testOr(null);
//        testWordBounders(null);
//        testFindwORD(null);
//        testFindwORD1(null);
        List<String> strings = Arrays.asList("A1B1", "A#02", "A111", "A002", "A03B01", "A03B02", "A4", "A3");
        strings = Arrays.asList("马鞍山10号", "马鞍山11号", "马鞍山12号", "马鞍山1号");
//        strings = Arrays.asList("0001#1", "0002#1", "0003#1", "0101#1");
        List<String> list = new ArrayList<>();
        for (String string : strings) {
            list.add(testFindAndReplace(string, 10));
        }
        list.sort((o1, o2) -> {
            return o1.compareTo(o2);
        });
        System.out.println("=================================================================================");
        list.stream().forEach(System.out::println);


    }

    public static String testFindAndReplace(String arg, final int bouder) {


        String pattern = "\\d+";
        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(arg);
        StringBuilder copy = new StringBuilder();
        int index = 0;
        while (m.find()) {
            copy.append(arg.substring(index, m.start()));
            index = m.end();
            copy.append(replaceMent(m.group(), bouder));
        }
        copy.append(arg.substring(index, arg.length()));
        return copy.toString();
    }

    private static String replaceMent(String part, int boud) {
        StringBuilder stringBuilder = new StringBuilder();
        IntStream.range(part.length(), boud).forEach(i -> {
            stringBuilder.append(0);
        });
        return stringBuilder.toString() + part;
    }

    public static void test1(String args) {
        Pattern pattern = Pattern.compile(myRex, Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
        Matcher matcher = pattern.matcher(myTest);
        if (matcher.find()) {
            String group = matcher.group();
            int start = matcher.start();
            int end = matcher.end();
            System.out.println(" match result is [" + group + "]  from " + start + " to " + end);
        } else {
            System.out.println("didn't match any string");
        }
    }

    public static void test(String args) {
        Pattern pattern = Pattern.compile(myRex);
        Matcher matcher = pattern.matcher(myTest);
        if (matcher.find()) {
            String group = matcher.group();
            int start = matcher.start();
            int end = matcher.end();
            System.out.println(" match result is [" + group + "]  from " + start + " to " + end);
        } else {
            System.out.println("didn't match any string");
        }
    }

    public static void testEmail(String args) {
        String myEmailTest = "from: to sssss  eeeeeeeeeeee  subject: jjjjj";
        String myEmailRex = "^(subject|from):";
        Pattern pattern = Pattern.compile(myEmailRex);
        Matcher matcher = pattern.matcher(myEmailTest);
        if (matcher.find()) {
            String group = matcher.group();
            int start = matcher.start();
            int end = matcher.end();
            System.out.println(" match result is [" + group + "]  from " + start + " to " + end);
        } else {
            System.out.println("didn't match any string");
        }
    }


    public static void testEmailjianjian(String args) {
        String myEmailTest = "from: to sssss  eeeeeeeeeeee  subject: jjjjj";
        String myEmailRex = "^[^sub]?";
        Pattern pattern = Pattern.compile(myEmailRex);
        Matcher matcher = pattern.matcher(myEmailTest);
        if (matcher.find()) {
            String group = matcher.group();
            int start = matcher.start();
            int end = matcher.end();
            System.out.println(" match result is [" + group + "]  from " + start + " to " + end);
        } else {
            System.out.println("didn't match any string");
        }
    }


    public static void testOr(String args) {
        String myEmailTest = "Jeffrey jeffery  ";
        String myEmailRex = "jeff(re|er)y";
        myEmailRex = "jeff[re][re]y";
        Pattern pattern = Pattern.compile(myEmailRex, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(myEmailTest);
        if (matcher.find()) {
            String group = matcher.group();
            int start = matcher.start();
            int end = matcher.end();
            System.out.println(" match result is [" + group + "]  from " + start + " to " + end);
        } else {
            System.out.println("didn't match any string");
        }
    }

    /**
     * java中似乎没有单词分解这个规则
     *
     * @param args
     */
    public static void testWordBounders(String args) {
        String myEmailTest = "jeffrey jeffery  ";
        String myEmailRex = "\\<jef";
        Pattern pattern = Pattern.compile(myEmailRex, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(myEmailTest);
        if (matcher.find()) {
            String group = matcher.group();
            int start = matcher.start();
            int end = matcher.end();
            System.out.println(" match result is [" + group + "]  from " + start + " to " + end);
        } else {
            System.out.println("didn't match any string");
        }
    }

    public static void testFindwORD1(String args) {
        // 按指定模式在字符串查找 pattern = "(\\D*)(\\d+)(.*)";
        String line = "color colour";
        String pattern = "colo[u]?r";
        boolean matches = line.matches(pattern);
        // 创建 Pattern 对象
        Pattern r = Pattern.compile(pattern);
        // 现在创建 matcher 对象
        Matcher m = r.matcher(line);
        while (m.find()) {
            System.out.println("index->" + " Found value: " + m.group());
        }
    }

    public static void testFindwORD(String args) {
        // 按指定模式在字符串查找 pattern = "(\\D*)(\\d+)(.*)";
        String line = "This order was placed for QT3000! OK?";
        String pattern = "\\d+";

        // 创建 Pattern 对象
        Pattern r = Pattern.compile(pattern);

        // 现在创建 matcher 对象
        Matcher m = r.matcher(line);
        if (m.find()) {
            int i = m.groupCount();
            IntStream.range(0, i + 1).forEach(index -> {
                System.out.println("index->" + index + " Found value: " + m.group(index));
            });
        } else {
            System.out.println("NO MATCH");
        }
    }


    public static void erere() {
        String a[] = {"aa10", "aa11", "aa2", "aa1"};
        String b[] = {"aaa10", "aaa11", "aaa2", "aaa1"};
        String temp;
        for (int i = 0; i < b.length - 1; i++) {
            for (int j = i + 1; j < b.length; j++) {
                if (b[i].compareTo(b[j]) > 0 || b[i].length() > b[j].length()) {
                    temp = b[i];
                    b[i] = b[j];
                    b[j] = temp;
                }
            }

        }


    }
}
